package com.ysd.lis.service.qc.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.qc.QcMaterialItemFixed;
import com.ysd.lis.mapper.qc.QcMaterialItemFixedMapper;
import com.ysd.lis.mapper.qc.QcMaterialItemResultMapper;
import com.ysd.lis.mapper.qc.QcMaterialMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.qc.QcMaterialFixedDto;
import com.ysd.lis.service.qc.QcMaterialItemFixedService;
import com.ysd.lis.service.qc.QcMaterialItemResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Service
@Slf4j
public class QcMaterialItemFixedServiceImpl extends ServiceImpl<QcMaterialItemFixedMapper, QcMaterialItemFixed> implements QcMaterialItemFixedService {

    @Autowired
    QcMaterialItemFixedMapper qcMaterialItemFixedMapper;
    @Autowired
    QcMaterialItemResultMapper qcMaterialItemResultMapper;
    @Autowired
    QcMaterialItemResultService qcMaterialItemResultService;
    @Autowired
    QcMaterialMapper qcMaterialMapper;



    @Override
    public Result findMaterialFixedList(CommonPageDto pageParam) {
        MPJLambdaWrapper<QcMaterialItemFixed> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QcMaterialItemFixed.class)

                .eq(QcMaterialItemFixed::getMaterialItemId,pageParam.getPid())
                .eq(QcMaterialItemFixed::getDelFlag,0)
                .orderByAsc(QcMaterialItemFixed::getSeq);
        List<QcMaterialItemFixed> fixedList = qcMaterialItemFixedMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", fixedList);
    }

    @Override
    public Result saveOrUpdateMaterialFixed(QcMaterialFixedDto item) {
        List<QcMaterialItemFixed> materialFixedList = item.getMaterialFixedList();
        List<QcMaterialItemFixed> updateFixedList = materialFixedList.stream().filter(ta -> StringUtils.isNotBlank(ta.getId())).collect(Collectors.toList());
        List<QcMaterialItemFixed> addFixedList = materialFixedList.stream().filter(ta -> StringUtils.isBlank(ta.getId())).collect(Collectors.toList());
        boolean b = this.saveOrUpdateBatch(materialFixedList);
        String jsJgFlag = item.getJsJgFlag();
//        if(ToolsUtils.isNotEmpty(jsJgFlag)&&jsJgFlag.equals("1")){
//            if(materialFixedList.size()>0){
//                for(QcMaterialItemFixed qcMaterialItemFixed : materialFixedList){
//                    //靶值的启用日期
//                    Timestamp enableDate = qcMaterialItemFixed.getEnableDate();
//                    Timestamp stopDate = qcMaterialItemFixed.getStopDate();
//                    //循环质控靶值，查出此阶段内有效期内质控物的结果
//                    MPJLambdaWrapper<QcMaterialItemResult> lambdaQueryWrapper2 = new MPJLambdaWrapper<>();
//                    lambdaQueryWrapper2.selectAll(QcMaterialItemResult.class)
//                            .innerJoin(QcMaterial.class,"rpt", p->p.eq(QcMaterial::getId,QcMaterialItemResult::getMaterialId)
//                                    .eq(QcMaterial::getDelFlag,0)
//                                    .le(QcMaterial::getEnableDate, new Timestamp(new Date().getTime()))
//                                    .ge(QcMaterial::getValidDate, new Timestamp(new Date().getTime()))
//                                    .and(wrapper -> wrapper.isNull(QcMaterial::getStopDate).or().ge(QcMaterial::getStopDate, new Timestamp(new Date().getTime()))))
//                            .eq(QcMaterialItemResult::getMaterialId,qcMaterialItemFixed.getMaterialId())
//                            .eq(QcMaterialItemResult::getMaterialItemId,qcMaterialItemFixed.getMaterialItemId())
//                            .ge(QcMaterialItemResult::getQcDate,enableDate);
//                    if(ToolsUtils.isNotEmpty(stopDate)){
//                        lambdaQueryWrapper2.le(QcMaterialItemResult::getQcDate,stopDate);
//                    }
//                    List<QcMaterialItemResult> resultList = qcMaterialItemResultMapper.selectList(lambdaQueryWrapper2);
//                }
//            }
//        }
        if(b){
            return Result.succ(1, "成功", null);
        }else {
            return Result.fail(400,"失败","");
        }

    }

    @Override
    public Result batchDeleteMaterialFixedList(QcMaterialFixedDto dto) {
        List<QcMaterialItemFixed> fixedList = dto.getMaterialFixedList();
        List<String> ids = fixedList.stream().map(QcMaterialItemFixed::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
